#ifndef WL_EXT_PLAYER_OPUS_H
#define WL_EXT_PLAYER_OPUS_H

typedef void* wl_ext_player_handle;
/**
 * 播放器的实现，基本流程
 * 开始，然后通知调用者各种事件。如果播放数据开始缺了，通过回调函数事件WL_EXT_PLAYER_DATA_DRAIN通知调用者缺少数据，
 * 调用者应该调用wl_ext_player_feed_data方法提供数据。建议实现者自己实现一个ringbuffer，当ringbuffer不足的时候,
 * 通知调用者缺少数据，然后调用wl_ext_player_feed_data方法提供数据。最后停止播放。
 */


/**
 * player event
 */
enum {
    WL_EXT_PLAYER_STARTED, // player has been started
    WL_EXT_PLAYER_PAUSED, // player has been paused
    WL_EXT_PLAYER_DATA_DRAIN, // player audio data has been drained, need more data
    WL_EXT_PLAYER_END, // player has been stopped, or play finished
    WL_EXT_PLAYER_ERROR // player error
};

/**
 * player callback
 * 
 * @param handle player handle
 * @param event player event
 * @param pContext player context
 */
typedef void (*wl_ext_player_callback)(wl_ext_player_handle handle, int event, void* pContext);

/**
 * init player
 * 
 * @param sampleRate sample rate
 * @return 0 success, < 0 fail
 */
int wl_ext_player_init(int sampleRate);

/**
 * deinit player
 * 
 * @return 0 success, < 0 fail
 */
int wl_ext_player_deinit(void);

/**
 * start player
 * 
 * @param pHandle player handle pointer, success it will return player handle, fail it will return NULL
 * @param pContext user context
 * @param callback callback
 * 
 * @return 0 success, < 0 fail
 */
wl_ext_player_handle wl_ext_player_start(void *pContext, wl_ext_player_callback callback);

/**
 * stop player
 * 
 * @param handle player handle
 *
 * @return 0 success, < 0 fail
 */
int wl_ext_player_stop(wl_ext_player_handle handle);

/**
 * feed data to player, it should drived by timer with 20ms also, or while callback get WL_EXT_PLAYER_DATA_DRAIN event,
 * it should be called to provide more pcm data.
 * 提供的是pcm的数据，按照字节pcmlen大小
 * 
 * @param handle player handle
 * @param pPcmData pcm data in byte format
 * @param pcmLen  bytes of pcm data 
 * @return > 0, means bytes actually read, < 0 means error
 */
int wl_ext_player_feed_data(wl_ext_player_handle handle, unsigned char *pPcmData, unsigned int pcmLen);

/**
 * pause player
 * 
 * @param handle player handle
 * 
 * @return 0 success, < 0 fail
 */
int wl_ext_player_pause(wl_ext_player_handle handle);

/**
 * resume player
 * 
 * @param handle player handle
 * 
 * @return 0 success, < 0 fail  
 */
int wl_ext_player_resume(wl_ext_player_handle handle);

#endif
